package netty.websocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @author shkstart
 * @create 2019-11-15 19:48
 */
public class WsChannelInitializer extends ChannelInitializer<SocketChannel> {


    protected void initChannel(SocketChannel socketChannel) throws Exception {
        //通过sockeChannel获取pipeline对象
        ChannelPipeline pipeline=socketChannel.pipeline();
        //添加了http的编码解码处理器
        pipeline.addLast(new HttpServerCodec());
        //处理大数据流
        pipeline.addLast(new ChunkedWriteHandler());
        //几乎所有的netty编程都会用到此handler
        pipeline.addLast(new HttpObjectAggregator(1024*64));
        //支持webSocket协议的处理器
        pipeline.addLast(new WebSocketServerProtocolHandler("ws"));

        pipeline.addLast(new ChatHandler());

         //心跳的支持
        //pipeline.addLast(new IdleStateHandler());
    }
}
